Module# 15: String Class Lecture#57: Application of String
// Example 57.1 : equals()
// Boolean
equals(Object o) Compares this string to the specified object.
public class StringEqualsDemo{
public static void main(String args[]){
String
text1 = "DATA STRUCTURE WITH JAVA";
String
text2 = "DATA STRUCTURE WITH C++";
String
text3 = "DATA STRUCTURE WITH JAVA";
boolean output1 = text1.equals(text2);
boolean output2 = text1.equals(text3);
System.out.println(output1);
System.out.println(output2);
}
}
// Example 57.2 :
equalsIgnoreCase()
// Boolean
equalsIgnoreCase(String s) Compares string to another string, ignoring case
considerations.
public class StringEqualsCaseDemo{
public static void main(String args[]){
String text1 = "DATA STRUCTURE
WITH JAVA";
String text2 = "DATA STRUCTURE
WITH C++";
String text3 = "DATA STRUCTURE
WITH JAVA";
String text4 = "data structure
with c++";
String text5 = "data structure
with java";
Boolean output1 = text1.equalsIgnoreCase(text2);
Boolean output2 = text1.equalsIgnoreCase(text3);
Boolean output3 = text1.equalsIgnoreCase(text4);
Boolean output4 = text1.equalsIgnoreCase(text5);
System.out.println(output1);
System.out.println(output2);
System.out.println(output3);
System.out.println(output4);
}
}
// Example 57.3 :
compareTo()
//int
compareTo(String s) Compares two string lexicographically.
public class StringCompareDemo {
public static void main(String args[]){
String text1 = "DATA STRUCTURE
WITH JAVA";
String text2 = "DATA STRUCTURE
WITH C++";
String text3 = "DATA STRUCTURE
WITH JAVA";
String text4 = "data structure
with c++";
String text5 = "data structure
with java";
int output1 = text1.compareTo(text2);
int output2 = text1.compareTo(text3);
int output3 = text1.compareTo(text4);
int output4 = text1.compareTo(text5);
System.out.println(output1);
System.out.println(output2);
System.out.println(output3);
System.out.println(output4);
}
}
// Example 57.4 :
compareToIgnoreCase()
// int
compareToIgnoreCase(String s) Compares two string lexicographically without
case matching.
public class
StringCompareCaseDemo{
public static void main(String args[]){
String text1 = "DATA STRUCTURE
WITH JAVA";
String text2 = "DATA STRUCTURE
WITH C++";
String text3 = "DATA STRUCTURE
WITH JAVA";
String text4 = "data structure
with c++";
String text5 = "data structure
with java";
int output1 = text1.compareToIgnoreCase(text2);
int output2 = text1.compareToIgnoreCase(text3);
int output3 = text1.compareToIgnoreCase(text4);
int output4 = text1.compareToIgnoreCase(text5);
System.out.println(output1);
System.out.println(output2);
System.out.println(output3);
System.out.println(output4);
}
}
// Example 57.5 :
substring()
// String
substring(int i) Returns the sub string starting from the character with i-th index.
public class SubstringDemo{
public static void main(String args[]){
String text = "DATA STRUCTURE
WITH JAVA";
String data = text.substring(3);
System.out.print(data);
}
}
// Example 57.6 :
substring()
// String
substring(int i, int j) Returns the substring from character with i to j-1
indices.
public class SubstringAnyDemo{
public static void main(String args[]){
String text = "DATA STRUCTURE
WITH JAVA";
String data = text.length(5,14);
System.out.print(data);
}
}
// Example 57.7 :
indexOf() method
// int indexOf(String
s) Returns the index within the string of the first occurrence of the specified
string.
public class StringSerachDemo{
public static void main(String args[]){
String text = "DATA STRUCTURE
WITH JAVA";
int output = text.indexOf("WITH");
System.out.print(output);
}
}
// Example 57.8 :
indexOf() method
// int indexOf(String
s, int i) Returns the index within the string of the first occurrence of the
specified string, starting at the specified index.
public class Example{
public static void main(String args[]){
String text = "DATA STRUCTURE
WITH JAVA";
int output = text.indexOf("T",4);
System.out.print(output);
}
}
// Example 57.9 :
lastIndexOf()
// int
lastIndexOf(String s) Returns the index within the string of the last
occurrence of the specified string.
public class Example{
public static void main(String args[]){
String text = "DATA STRUCTURE
WITH JAVA";
int output = text.lastIndexOf("T");
System.out.print(output);
}
}
// Example 57.10 :
String to Integer
public class StringToIntegerDemo{
public static void main(String args[]){
String number = "95";
int num = Integer.parseInt(number);
int output = num + 5;
System.out.println(output);
}
}
// Example 57.11 :
String to Float
// public class
StringToFloatDemo{
public static void main(String args[]){
String number = "95.59";
float num = Float.parseFloat(number);
float output = num + (float)4.41;
System.out.println(output);
}
}
// Example 57.12 :
String to Double
public class StringToDoubleDemo{
public static void main(String args[]){
String number = "95.59";
double num = Double.parseDouble(number);
double output = num + 4.41;
System.out.println(output);
}
}
// Example 57.13 :
Integer to String
public class IntegerToStringDemo{
public static void main(String args[]){
int i=20;
String s=String.valueOf(i);
System.out.println(i+10); //300 because + is
binary plus operator
System.out.println(s+20); //200100 because + is
string concatenation operator
}
}
// Example 57.14 :
Float to String
public class FloatToStringDemo{
public static void main(String args[]){
float f=15.5F;//F is the suffix for float
String s = String.valueOf(f);
System.out.println(s);
}
}
// Example 57.15 :
String other format conversion
// The following
program demonstrates binary, hexadecimal, and octal conversion:
class StringConversions {
public static void main(String args[]) {
int num = 19648;
System.out.println(num + " in binary: " +
Integer.toBinaryString(num));
System.out.println(num + " in octal: " +
Integer.toOctalString(num));
System.out.println(num + " in hexadecimal: " +
Integer.toHexString(num));
}
}
// Example 57.16: Numbers as command line
arguments
class CommandLineDemo2 {
public static void main(String args[]) throws IOException {
System.out.println(“Number of arguments”
“ + args.length);
int sum = 0;
if(args.length == 0)
return;
for(int i=0; i<args.length; i++)
sum += Integer.parseInt(args[i]);
System.out.println(“Sum = “ + sum);
}
}